#!/usr/bin/env python
# encoding: utf-8
"""
sampledownloader.py

This application is a simple application that allows you to download
a single file from your Nirvanix account.

Created by Barry Ruffner on 2010-09-08.
Copyright (c) 2008-2010 Nirvanix. All rights reserved.
"""

import sys
import os
import os.path
from nirvanix import NirvanixDownloader
from nirvanix import Nirvanix
from nirvanix import Session
import nirvanixutils

def main():
	args = sys.argv[1:]
	if len(args) < 5:
		print "\r\nUsage: AppKey Username Password NirvanixFilePath LocalPath [NumberOfThreads]"
		print "The number of threads is an optional value."
		print "Example: python sampledownloader.py XXXX-XXXX-XXXX MyUser MyPassword /downloaddir/file.txt /localdir/\r\n"
		return
	# assign args to nice names
	appKey = args[0]
	username = args[1]
	password = args[2]
	nvxpath = args[3]
	localpath = args[4]
	if len(args) == 6:
		number_of_threads = args[5]
	else:
		number_of_threads = 1

	# Create a session object
	try:
		session = Session(appKey, username, password)
	except nirvanixutils.NirvanixException, e:
		if e.code == 80001:
			print "Login Failed, Please correct the username and password you provided and try again."
			return
		else:
			print "The following error occured while logging in: " + e.message

	# Create an uploader object with ShowProgress = true
	try:
		downloader = NirvanixDownloader(session, True, number_of_threads)
		downloader.downloadfile(localpath, nvxpath)
		print "\r\n"
	except nirvanixutils.NirvanixException, e:
		print e
		print e.code
		return
	
if __name__ == '__main__':
	main()